<template>
	<view class="p-cneter" style="padding: 0px 30px 0px 30px;">
		<view style="text-align: center;" class="img-limt">
			<image src="../../static/logo.png" mode='widthFix'></image>
		</view>
		<view>
			<view class="bl b10">
				<view class="bc bk r10">
					<i class="ri-smartphone-line" style="color: #008040;font-size:16px;"></i>
					<!-- <i-icon size="16px" color="#008040" name="tablet-line"></i-icon> -->
					<input type="text" placeholder="手机号" v-model="phone" maxlength="11" />
				</view><button @click="getVerify()" :disabled="codedisabled" style="font-size: 10px;">{{title}}</button>
			</view>
			<view class="bc bk b30">
				<i class="ri-lock-line" style="color: #008040;font-size:16px;"></i>
				<!-- <i-icon size="16px" color="#008040" name="lock-line"></i-icon> -->
				<input type="text" placeholder="验证码" v-model="verifyCode" maxlength="4" />
			</view>
			<view class="b10">
				<button @click="login()">
					<i class="ri-smartphone-line" style="color: #008040;font-size:16px;"></i>
					<!-- <i-icon size="16px" color="#FFF" name="tablet-line"></i-icon> -->
					登录
				</button>
			</view>
		</view>
		<view class="bc" style="font-size: 12px;">
			<checkbox @click="selectCk" />
			<span>我已阅读并接受《用户服务协议》</span>
		</view>
		<view class="footer">
			技术支持：广西奇果网络科技有限公司
		</view>
	</view>
</template>

<script>
	import zcSwiper from '@/components/zc-swiper/zc-swipe.vue'
	export default {
		data() {
			return {
				phone: '',
				verifyCode: '',
				siteId: '', //站点id
				codedisabled: false,
				isRead: false,
				title: '获取验证码',
				timer: 60,
				list: []
			}
		},
		components: {
			'zc-swiper': zcSwiper
		},
		onLoad(item) {
			console.log(item.id)
			if (item.id == null || item.id == '' || item.id == undefined) {
				this.GetRequest(location.href);
			} else {
				this.siteId = item.id;
			}
		},
		methods: {
			GetRequest(value) {
				console.log(value)
				var object = {};
				if (value.indexOf("?") != -1) { //url中存在问号，也就说有参数。  
					let indx = value.indexOf("?");
					var str = value.substr(indx + 4); //得到?后面的字符串
				}
				this.siteId = str;
				console.log(this.siteId)
			},
			getVerify: function() {
				if (!this.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				};
				if (this.timer == 60) {
					this.codedisabled = true

					let timer1 = setInterval(() => {
						this.timer--;
						this.title = this.timer + '秒后重试'
						if (this.timer == 0) {
							clearInterval(timer1);
							this.timer = 60;
							this.codedisabled = false
							this.title = '获取验证码'
							return;
						}
					}, 1000);
				}
				uni.request({
					url: this.baseURL + '/send/code?phone=' + this.phone + '&type=0',
					method: "GET",
					dataType: 'json',
					success: function(res) {
						console.log(res);
						uni.showToast({
							title: '发送成功',
							icon: 'success',
							duration: 2000
						});
					},
					fail: function(err) {
						console.log(err);
						uni.showToast({
							title: '发送错误,' + err.message,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			login: function() {
				let _this = this;
				if(_this.siteId == '' || _this.siteId == undefined){
					uni.showToast({
						title: '未获取所属站点',
						icon: 'none'
					});
					return;
				}
				else if (!_this.phone || !_this.verifyCode) {
					uni.showToast({
						title: '请输入手机号和验证码',
						icon: 'none'
					});
					return;
				} else if (_this.isRead === false) {
					uni.showModal({
						content: '请阅读并同意《用户服务协议》',
						showCancel: false
					})
					return
				}
				uni.showLoading({
					title: "登录中..."
				})
				uni.request({
					url: _this.baseURL + '/gm/code/login',
					method: 'POST',
					header: {
						"content-type": "application/json;charset=utf-8"
					},
					data: {
						stationId:_this.siteId,
						phone: _this.phone,
						VerifyCode:_this.verifyCode,
						password: new Date().getTime().toString()
					},
					success(res) {
						console.log(res)
						uni.hideLoading()
						if (res.data.success == true) {
							uni.setStorageSync('token', res.data.data)
							uni.showToast({
								title: "登录成功",
								duration: 2000,
								icon: 'loading'
							})
							setTimeout(function() {
								uni.redirectTo({
									url: 'index'
								})
							}, 2000);
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'error'
							})
						}
					},
					fail(err) {
						console.log(err)
					}
				})
			},
			selectCk: function() {
				this.isRead = this.isRead === true ? false : true;
			}
		}
	}
</script>